#include <Time.h>

//Drafted during class lecture, morning of 20100818
//Tom Bertalan : Tom@TomBertalan.com : www.TomBertalan.com

//My grandpa lives on his own in California. One of his daughters lives nearby,
//though, and drives past his house each day. The idea is to put an IR sensor 
//somewhere he'll pass it each day. There will normally be a green 110V light 
//showing where my aunt can see it, but, if he does not trip the sensor with in 
//some dial-settable period (like 24 hours), the light will go red, and my aunt 
//will know to investigate.

//Uses the Arduino prototyping board. (www.Arduino.cc)



//PIN DEFINITIONIS
//Should these be constants instead?
#include "WProgram.h"
void setup();
void checktimedial(void);
void checktime(void);
void checkir(void);
void setlights(void);
void loop();
int dialpin			= 0; //Pin numbers are arbitrarily chosen, for now.
int irpin			= 2;
int redledpin		= 3;
int greenledpin		= 4;
int relaypin		= 5;

//VARIABLE DEFINITIONS
//Or should it be "variable constructors"?
int dialread            = 0;    //This is retarded.
int dialsetting		= 1;	//Will immediately be overwritten by checktimedial()
int endtimes		= 100;	//also will soon be overwritten, but 0 might cause wierd behavior. I should try it.
int triggerlevel	= 512;	//Needs to be tailored to the sensor chosen. Could be overwritten by a checktriggerdial.
              //And, while we're talking about possible features, I should mention that I'm
              //having some amorphous thoughts about a sort of vacation button.
int irread              = LOW;
int lightcolor	= 0;
float dialscale = 0.25; //168 gives a max timeout of 48 hours
float timeout = 0;
float timeouthr = 0;

void setup()		//What's up with the order of sections in these example sketches (see arduino.cc)?
          //They always seem to include:
          //	1. lots of ints at the beginning
          //	2. Usually, some functions defined.
          //	3. a void setup()
          //	4. a void loop(), where the program actually runs
          //The thing is, I think I've seen some examples (not on the arduino.cc site, and not
          //necessesarily in wiring, but definitely in some variant of C) that had some functions
          //used in a loop() or main() that weren't defined until after that loop(). I thought
          //this was impossible. Is this a http://en.wikipedia.org/wiki/False_memory_syndrome of mine?
{
  //pinMode(dialpin, INPUT); this pin is analog
  pinMode(irpin, INPUT);
  pinMode(redledpin, OUTPUT);
  pinMode(greenledpin, OUTPUT);
  pinMode(relaypin, OUTPUT);
  Serial.begin(9600);
}

void checktimedial(void)
{
  dialread = analogRead(dialpin);
  if(dialread != dialsetting)
  {
    dialsetting = analogRead(dialpin);
    //endtimes should not be set here, but only in checkir().
    timeout = dialsetting * dialscale;
    timeouthr = timeout/3600;
    Serial.print("timeout is ");
    Serial.println(timeout);
  }
}

void checktime(void)				//I'm using this correctly, right? the bool bit at the beginning is supposed to sepcify
                  //the return type, and the void part inside is supposed to specify the argument(s) type.
                  //for that matter, did I do it right with setlighst() below? Do I have to do 
                  //void setlights(string lightcolor), or is defining the datatype there unnecessary, since
                  //I defined it at the top of the sketch?
{
//  Serial.print("now is ");
//  Serial.println(now());
  if(now() >= endtimes)
  {
    lightcolor = 0;
  }
  else
  {
    lightcolor = 1;
  }
}

void checkir(void)
{
  irread = digitalRead(irpin);
  if(irread == HIGH)
  {
//    Serial.println("I see you there.");
      endtimes = now() + timeout;
//    Serial.print("endtimes is ");
//    Serial.print(endtimes);
//    Serial.println(" via irread");
  }
//  else
//  {
//    Serial.println("WTF. No one there.");
//  }
}

void setlights(void)
{
  if(lightcolor == 1)
  {
    digitalWrite(greenledpin, HIGH);
    digitalWrite(redledpin, LOW);
    digitalWrite(relaypin, HIGH);	//HIGH means the relay will be in it's non-normal state.
                    //I'm making this the green light, so that, if something happens to the board, the
                    //(separately powered) 110v lights will switch to red.
  }
  else
  {
    digitalWrite(greenledpin, LOW);
    digitalWrite(redledpin, HIGH);
    digitalWrite(relaypin, LOW);	//Now that I think about it, it would be a good idea to check what the actual voltate is
                    //that's associated with LOW. I'm hoping it's ~=0V. HIGH, I'm pretty sure, is 5V, but
                    //I should, of course, check that too.
  }
}

void loop()
{
  checktimedial();
  checktime();
  checkir();
  setlights();
}

int main(void)
{
	init();

	setup();
    
	for (;;)
		loop();
        
	return 0;
}

